package com.srtp.secureprotocol.controller

import com.srtp.secureprotocol.main.SecureProtocol
import com.srtp.secureprotocol.serializable.req.GeneratePreKeysRequest
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
import io.ktor.server.application.ApplicationCall
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.response.respondText

class KeyMangeController {
    suspend fun refreshIdentityKey(
        secureProtocol: SecureProtocol,
        call: ApplicationCall
    ) {
        try {
            val json = call.receive<Map<String, Any>>()
            val identityKeyAge = (json["identityKeyAge"] as Number).toLong()

            val result = secureProtocol.refreshIdentityKey(identityKeyAge)
            if (result == null) {
                call.respond(mapOf("status" to "notExpired", "message" to "Identity key is still valid"))
            } else {
                call.respondText(result.toString(), ContentType.Application.Json)
            }
        } catch (e: Exception) {
            call.respond(HttpStatusCode.BadRequest, mapOf("status" to "error", "message" to e.message))
        }
    }

    suspend fun refreshSignedPreKey(
        secureProtocol: SecureProtocol,
        call: ApplicationCall
    ) {
        try {
            val json = call.receive<Map<String, Any>>()
            val signedPreKeyAge = (json["signedPreKeyAge"] as Number).toLong()

            val result = secureProtocol.refreshSignedPreKey(signedPreKeyAge)
            if (result == null) {
                call.respond(mapOf("status" to "notExpired", "message" to "Signed pre-key is still valid"))
            } else {
                call.respondText(result.toString(), ContentType.Application.Json)
            }
        } catch (e: Exception) {
            call.respond(HttpStatusCode.BadRequest, mapOf("status" to "error", "message" to e.message))
        }
    }


    suspend fun generatePreKeys(
        request: GeneratePreKeysRequest,
        secureProtocol: SecureProtocol,
        call: ApplicationCall
    ) {
        try {
            val nextPreKeyId = request.nextPreKeyId
            val count = request.count

            val result = secureProtocol.generatePreKeys(nextPreKeyId, count)
            call.respondText(result.toString(), ContentType.Application.Json)
        } catch (e: Exception) {
            call.respond(HttpStatusCode.BadRequest, mapOf("status" to "error", "message" to e.message))
        }
    }

    suspend fun reEncryptKeys(
        secureProtocol: SecureProtocol,
        call: ApplicationCall
    ) {
        try {
            val json = call.receive<Map<String, Any>>()
            val password = json["password"] as String

            val result = secureProtocol.reEncryptKeys(password, null)
            call.respondText(result.toString(), ContentType.Application.Json)
        } catch (e: Exception) {
            call.respond(HttpStatusCode.BadRequest, mapOf("status" to "error", "message" to e.message))
        }
    }

}